package com.yanfan.zutai.controller;

import com.yanfan.common.annotation.Log;
import com.yanfan.common.core.controller.BaseController;
import com.yanfan.common.core.domain.AjaxResult;
import com.yanfan.common.core.page.TableDataInfo;
import com.yanfan.common.enums.BusinessType;
import com.yanfan.common.utils.poi.ExcelUtil;
import com.yanfan.zutai.aop.SysInitData;
import com.yanfan.zutai.aop.SysInitUpdateData;
import com.yanfan.zutai.aop.SysQueryData;
import com.yanfan.zutai.domain.BDeviceTime;
import com.yanfan.zutai.service.IBDeviceTimeService;
import com.yanfan.zutai.util.Detect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 设备定时Controller
 *
 * @author ruoyi
 * @date 2021-12-07
 */
@RestController
@RequestMapping("/zutai/bDeviceTime")
public class BDeviceTimeController extends BaseController {
    @Autowired
    private IBDeviceTimeService bDeviceTimeService;

    /**
     * 查询设备定时列表
     */
    //@PreAuthorize("@ss.hasPermi('ghxx:bDeviceTime:list')")
    @GetMapping("/list")
    @SysQueryData
    public TableDataInfo list(BDeviceTime bDeviceTime) {
        startPage();
        List<BDeviceTime> list = bDeviceTimeService.selectBDeviceTimeList(bDeviceTime);
        for (int i = 0; i < list.size(); i++) {
            BDeviceTime bDeviceTimeQuery = list.get(i);
            if (bDeviceTimeQuery.getWeek() != null) {
                String[] weeks = bDeviceTimeQuery.getWeek().split(",");
                bDeviceTimeQuery.setWeeks(weeks);
            }
            if (bDeviceTimeQuery.getMonth() != null) {
                String[] months = bDeviceTimeQuery.getMonth().split(",");
                bDeviceTimeQuery.setMonths(months);
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出设备定时列表
     */
    //@PreAuthorize("@ss.hasPermi('ghxx:bDeviceTime:export')")
    @Log(title = "设备定时", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    @SysQueryData
    public AjaxResult export(BDeviceTime bDeviceTime) {
        List<BDeviceTime> list = bDeviceTimeService.selectBDeviceTimeList(bDeviceTime);
        ExcelUtil<BDeviceTime> util = new ExcelUtil<BDeviceTime>(BDeviceTime.class);
        return util.exportExcel(list, "bDeviceTime");
    }

    /**
     * 获取设备定时详细信息
     */
    //@PreAuthorize("@ss.hasPermi('ghxx:bDeviceTime:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        BDeviceTime bDeviceTimeQuery = bDeviceTimeService.selectBDeviceTimeById(id);
        if (bDeviceTimeQuery.getWeek() != null) {
            String[] weeks = bDeviceTimeQuery.getWeek().split(",");
            bDeviceTimeQuery.setWeeks(weeks);
        }
        if (bDeviceTimeQuery.getMonth() != null) {
            String[] months = bDeviceTimeQuery.getMonth().split(",");
            bDeviceTimeQuery.setMonths(months);
        }
        return AjaxResult.success(bDeviceTimeQuery);
    }

    /**
     * 新增设备定时
     */
    //@PreAuthorize("@ss.hasPermi('ghxx:bDeviceTime:add')")
    @Log(title = "设备定时", businessType = BusinessType.INSERT)
    @PostMapping
    @SysInitData
    public AjaxResult add(@RequestBody BDeviceTime bDeviceTime) {
        StringBuilder stringBuilder = new StringBuilder();
        if (bDeviceTime.getMonths() != null) {
            for (int i = 0; i < bDeviceTime.getMonths().length; i++) {
                if (Detect.notEmpty(bDeviceTime.getMonths()[i])) {
                    stringBuilder.append(bDeviceTime.getMonths()[i] + ",");
                    bDeviceTime.setMonth(stringBuilder.toString());
                }
            }
        }
        if (bDeviceTime.getWeeks() != null) {
            stringBuilder.delete(0, stringBuilder.length());
            for (int i = 0; i < bDeviceTime.getWeeks().length; i++) {
                if (Detect.notEmpty(bDeviceTime.getWeeks()[i])) {
                    stringBuilder.append(bDeviceTime.getWeeks()[i] + ",");
                    bDeviceTime.setWeek(stringBuilder.toString());
                }
            }
        }
        bDeviceTimeService.insertBDeviceTime(bDeviceTime);
        return AjaxResult.success();
    }

    /**
     * 修改设备定时
     */
    //@PreAuthorize("@ss.hasPermi('ghxx:bDeviceTime:edit')")
    @Log(title = "设备定时", businessType = BusinessType.UPDATE)
    @PutMapping
    @SysInitUpdateData
    public AjaxResult edit(@RequestBody BDeviceTime bDeviceTime) {
        StringBuilder stringBuilder = new StringBuilder();
        if (bDeviceTime.getMonths() != null) {
            for (int i = 0; i < bDeviceTime.getMonths().length; i++) {
                if (Detect.notEmpty(bDeviceTime.getMonths()[i])) {
                    stringBuilder.append(bDeviceTime.getMonths()[i] + ",");
                    bDeviceTime.setMonth(stringBuilder.toString());
                }
            }
        }
        if (bDeviceTime.getWeeks() != null) {
            stringBuilder.delete(0, stringBuilder.length());
            for (int i = 0; i < bDeviceTime.getWeeks().length; i++) {
                if (Detect.notEmpty(bDeviceTime.getWeeks()[i])) {
                    stringBuilder.append(bDeviceTime.getWeeks()[i] + ",");
                    bDeviceTime.setWeek(stringBuilder.toString());
                }
            }
        }
        return toAjax(bDeviceTimeService.updateBDeviceTime(bDeviceTime));
    }

    /**
     * 删除设备定时
     */
    //@PreAuthorize("@ss.hasPermi('ghxx:bDeviceTime:remove')")
    @Log(title = "设备定时", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(bDeviceTimeService.deleteBDeviceTimeByIds(ids));
    }
}
